﻿using HDJ.Framework.Core;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace HDJ.Framework.Core
{
    public static class UGUITool
    {

        public static UIRoot CreateUIRoot(int rootID)
        {
            GameObject gameObject = new GameObject("[UIRoot_" + rootID + "]");

            UIRoot uIRoot = gameObject.AddComponent<UIRoot>();
            uIRoot.id = rootID;

            uIRoot.childRoot = CreateStretchRectTransform(gameObject.transform, "Root");
            uIRoot.childRoot.localPosition = new Vector3(rootID * 10000, uIRoot.childRoot.localPosition.y, uIRoot.childRoot.localPosition.z);

            gameObject.AddComponent<RectTransform>();

            Canvas can = gameObject.AddComponent<Canvas>();
            can.renderMode = RenderMode.ScreenSpaceCamera;
            can.pixelPerfect = true;

            gameObject.AddComponent<GraphicRaycaster>();


            GameObject camObj = new GameObject("UICamera");
            camObj.transform.parent = uIRoot.childRoot;
            camObj.transform.localPosition = new Vector3(0, 0, -100f);
            Camera cam = camObj.AddComponent<Camera>();
            cam.clearFlags = CameraClearFlags.Depth;
            cam.orthographic = true;
            cam.farClipPlane = 200f;
            can.worldCamera = cam;
            cam.cullingMask = 1 << 5;
            cam.nearClipPlane = -50f;
            cam.farClipPlane = 50f;
            cam.depth = rootID;

            uIRoot.uiCamera = cam;

            CanvasScaler cs = gameObject.AddComponent<CanvasScaler>();
            cs.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
            cs.referenceResolution = new Vector2(1920f, 1080f);
            cs.screenMatchMode = CanvasScaler.ScreenMatchMode.Expand;

            uIRoot.normalRoot = CreateStretchRectTransform(uIRoot.childRoot, "NormalRoot");
            uIRoot.normalRoot.transform.localScale = Vector3.one;

            uIRoot.fixedRoot = CreateStretchRectTransform(uIRoot.childRoot, "FixedRoot");
            uIRoot.fixedRoot.transform.localScale = Vector3.one;

            uIRoot.popupRoot = CreateStretchRectTransform(uIRoot.childRoot, "PopupRoot");
            uIRoot.popupRoot.transform.localScale = Vector3.one;

            Transform[] tras = UnityExtendUtils.GetDeepChildTransforms(uIRoot.transform);
            foreach (var item in tras)
            {
                item.gameObject.layer = LayerMask.NameToLayer("UI");
            }

            CreateEventSystem();

            return uIRoot;
        }

        public static void CreateEventSystem()
        {
            //add Event System
            GameObject eventObj = GameObject.Find("EventSystem");
            if (eventObj == null)
            {
                eventObj = new GameObject("EventSystem");
                eventObj.AddComponent<EventSystem>();
                eventObj.AddComponent<StandaloneInputModule>();
            }
        }
        public static Transform CreateStretchRectTransform(Transform root, string name)
        {
            GameObject go = new GameObject(name);
            go.transform.parent = root;
            RectTransform rect = go.AddComponent<RectTransform>();
            rect.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, 0, 0);
            rect.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0, 0);
            rect.anchorMin = Vector2.zero;
            rect.anchorMax = Vector2.one;
            return go.transform;
        }
        /// <summary>
        /// 设置RectTransform全屏拉伸，并且其他参数Reset
        /// </summary>
        /// <param name="obj"></param>
        public static void SetRectTransformScreen(GameObject obj)
        {
            RectTransform rect = obj.GetComponent<RectTransform>();
            if (rect == null)
            {
                rect = obj.AddComponent<RectTransform>();
            }
            rect.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, 0, 0);
            rect.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0, 0);
            rect.anchorMin = Vector2.zero;
            rect.anchorMax = Vector2.one;
            rect.localScale = Vector3.one;
        }
    }
}
